package org.example.ui.customer;

import org.example.business.*;
import org.example.ui.administrator.ManageUserPanel;
import org.example.ui.administrator.MilkTeaAdminItem;
import org.example.ui.administrator.MilkTeaAdminPanel;
import org.example.ui.employee.ManageOrderPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BottomPanel extends JPanel {
    private final JLabel programName;
    private final SpringLayout layout;
    private final Font font;
    private static JPanel containerPanel;
    private static CardLayout cardLayout;
    private JMenuBar menuBar;
    private final String[] array = {"首页", "点餐", "订单", "订单管理", "奶茶管理", "用户管理"};
    public static JButton[] button;
    private static int n = 0;
    public BottomPanel(){
        layout = new SpringLayout();
        Image image = new ImageIcon("data/background/bag_icon.jpg")
                .getImage().getScaledInstance(120,120, Image.SCALE_DEFAULT);
        button = new JButton[array.length];
        containerPanel = new JPanel();
        containerPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        cardLayout = new CardLayout();
        programName = new JLabel("<html>" + "霸王" + "<br/>" + "茶坤" + "<html/>");
        programName.setIcon(new ImageIcon(image));
        font = new Font("行楷",Font.BOLD,25);
        setMenuBar();
        setLocation();
        setAllListener();
        setALL();
        addAll();
    }
    public void setLocation(){
        layout.putConstraint(SpringLayout.EAST, menuBar, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.NORTH, menuBar, 10, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.SOUTH, menuBar, 40, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, containerPanel,170, SpringLayout.WEST,this);
        layout.putConstraint(SpringLayout.NORTH, containerPanel,0, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.SOUTH, containerPanel,0, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.EAST, containerPanel,0, SpringLayout.EAST,this);
        layout.putConstraint(SpringLayout.EAST, programName,0, SpringLayout.WEST, containerPanel);
        layout.putConstraint(SpringLayout.WEST, programName,0, SpringLayout.WEST,this);
        layout.putConstraint(SpringLayout.NORTH, programName,0, SpringLayout.NORTH,this);
        if (UserBusiness.user.identity.equals("管理员")) {
            n = 6;
        }
        else if (UserBusiness.user.identity.equals("店员")) {
            n = 4;
        }
        else {
            n = 3;
        }
        for (int i = 0; i < n; i++) {
            button[i] = new JButton(array[i]);
            button[i].setFont(font);
            button[i].setBackground(Color.WHITE);
            add(button[i]);
            if (i == 0) {
                layout.putConstraint(SpringLayout.NORTH, button[i],50, SpringLayout.SOUTH,programName);
            }
            else {
                layout.putConstraint(SpringLayout.NORTH, button[i],0, SpringLayout.SOUTH, button[i-1]);
            }
            layout.putConstraint(SpringLayout.EAST, button[i],0, SpringLayout.WEST, containerPanel);
            layout.putConstraint(SpringLayout.WEST, button[i],0, SpringLayout.WEST,this);
        }
    }
    private void setALL(){
        setLayout(layout);
        setSize(700,600);
        containerPanel.setLayout(cardLayout);
        containerPanel.setBackground(null);
        containerPanel.setOpaque(false);
        programName.setFont(new Font("华文行楷",Font.ITALIC|Font.BOLD,25));
        programName.setBackground(Color.ORANGE);
        button[0].setBackground(Color.CYAN);
        setVisible(true);
    }
    private void setAllListener(){
        for (int i = 0; i < n; i++) {
            int finalI = i;
            button[i].addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    for (int j = 0; j < n; j++) {
                        if (j != finalI) {
                            button[j].setBackground(Color.WHITE);
                        }
                        else {
                            button[j].setBackground(Color.CYAN);
                        }
                    }
                    cardLayout.show(containerPanel,String.valueOf(finalI + 1));
                }
            });
        }
    }
    private void setMenuBar() {
        Image image = new ImageIcon("data/" + UserBusiness.user.avatarPath).getImage()
                .getScaledInstance(30, 30, Image.SCALE_DEFAULT);
        menuBar = new JMenuBar();
        JMenu menu = new JMenu("账号");
        JMenuItem item = new JMenuItem(UserBusiness.user.name, new ImageIcon(image)),
                itemOne = new JMenuItem("切换账号", JMenuItem.WEST),
                itemTow = new JMenuItem("退出程序", JMenuItem.WEST),
                itemThree = new JMenuItem("账号设置", JMenuItem.CENTER);
        JButton refreshButton = new JButton("\uD83D\uDD04");
        refreshButton.setFont(font);
        refreshButton.setBackground(Color.WHITE);
        UserBusiness.item = item;
        itemThree.setFont(font);
        item.setFont(font);
        itemOne.setFont(font);
        itemTow.setFont(font);
        menu.setFont(font);
        menuBar.add(menu);
        menuBar.add(refreshButton);
        itemTow.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        itemOne.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                LoginBusiness.lock.unLock();
            }
        });
        itemThree.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                LoginBusiness.layout.show(LoginBusiness.panel, "2");
            }
        });
        refreshButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        MilkTeaBusiness.setMilkTeas(0);
                        MilkTeaBusiness.setKinds();
                        OrderBusiness.updateOrders();
                        if (UserBusiness.user.identity.equals("管理员")) {
                            MilkManageBusiness.setAdminItems(0);
                            UserManageBusiness.setUserItems();
                            ManageOrderPanel.setAdminItems();
                            MilkTeaAdminItem.isSave = true;
                        }
                        if (UserBusiness.user.identity.equals("店员")) {
                            ManageOrderPanel.setAdminItems();
                        }
                        LoginBusiness.layout.show(LoginBusiness.panel, "1");
                    }
                }).start();
                LoginBusiness.layout.show(LoginBusiness.panel, "0");
                LoginBusiness.panel.setVisible(false);
                LoginBusiness.panel.setVisible(true);
            }
        });
        menu.add(item);
        menu.add(itemOne);
        menu.add(itemThree);
        menu.add(itemTow);
    }
    private void addAll(){
        containerPanel.add(new HomePanel(),"1");
        containerPanel.add(new OrderPanel(),"2");
        containerPanel.add(new HistoryOrderPanel(),"3");
        if (UserBusiness.user.identity.equals("管理员")) {
            containerPanel.add(new ManageOrderPanel(), "4");
            containerPanel.add(new MilkTeaAdminPanel(), "5");
            containerPanel.add(new ManageUserPanel(), "6");
        }
        else if (UserBusiness.user.identity.equals("店员")) {
            containerPanel.add(new ManageOrderPanel(), "4");
        }
        add(programName);
        add(menuBar);
        add(containerPanel);
    }
    public static void moreOrder() {
        for (int j = 0; j < n; j++) {
            if (j != 1) {
                button[j].setBackground(Color.WHITE);
            }
            else {
                button[j].setBackground(Color.CYAN);
            }
        }
        cardLayout.show(containerPanel,String.valueOf( 2));
    }
    @Override
    public void paintComponent(Graphics g) {
        ImageIcon imageIcon = new ImageIcon("data/background/微信图片_20241127230536.jpg");
        g.drawImage(imageIcon.getImage(), 0, 0, getWidth(), getHeight(), this);
    }
}
